﻿<%@ WebHandler Language="C#" Class="Processor" %>

using System;
using System.IO;
using System.Net;
using System.Web;
using System.Xml;
using System.Xml.Xsl;

public class Processor : IHttpHandler 
{
    private const string API_URL = @"http://yp.shoutcast.com/sbin/newxml.phtml?search={0}";
    private const string RADIO_URL = @"http://www.shoutcast.com/sbin/tunein-station.pls?id={0}";
    private const string STYLE_SHEET_PATH = @"~/SearchResults.xslt";
    
    public void ProcessRequest (HttpContext context) 
    {
        string response = string.Empty;
        string method = context.Request["m"];
        string data = context.Request["d"];

        if (method != null && data != null)
        {
            switch (method.ToUpper())
            {
                case "SEARCH":
                    response = GetStyledSearchResult(data, context.Server.MapPath(STYLE_SHEET_PATH));
                    break;
                case "RADIO":
                    response = GetRadioURL(data);
                    break;
            }
        }
        
        context.Response.ContentType = "text/html";
        context.Response.Write(response);
    }
 
    public bool IsReusable 
    {
        get { return false; }
    }

    private string GetStyledSearchResult(string searchFor, string stylePath)
    {
        string result = string.Empty;

        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(API_URL, searchFor.Replace(" ", "%20")));

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
                {
                    StringWriter writer = new StringWriter();
                    XslCompiledTransform trans = new XslCompiledTransform();

                    trans.Load(stylePath, XsltSettings.TrustedXslt, null);
                    trans.Transform(reader, null, writer);
                    result = writer.ToString().Trim();
                }
            }
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }

        if (result.Length > 0)
            return "<ul>" + result + "</ul>";
        else
            return result;
    }

    private string GetRadioURL(string radioId)
    {
        string result = string.Empty;
        string line;

        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(RADIO_URL, radioId));

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (line.IndexOf("File") > -1)
                        {
                            result = line.Substring(line.IndexOf("=") + 1);
                            break;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }

        return result;
    }
}